package cn.edu.njuit.children.common;

import lombok.Data;

import java.io.Serializable;

/**
 * @Author guzy
 * @Description 返回结果
 * @Date 2022/4/26 20:35
 **/
@Data
public class Result implements Serializable {

    private Integer code;

    private boolean success = true;

    private String msg;

    private Object data;

    public Result() {
    }

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static Result success() {
        Result result = new Result();
        result.setSuccess(true);
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }

    public static Result success(Object data) {
        Result result = new Result();
        result.setSuccess(true);
        result.setResultCode(ResultCode.SUCCESS);
        result.setData(data);
        return result;
    }

    public static Result success(String msg,Object data) {
        Result result = new Result();
        result.setSuccess(false);
        result.setMsg(msg);
        result.setCode(ResultCode.SUCCESS.code());
        result.setData(data);
        return result;
    }

    public static Result failure(ResultCode resultCode) {
        Result result = new Result();
        result.setSuccess(false);
        result.setResultCode(resultCode);
        result.setData(null);
        return result;
    }

    public static Result failure(String msg) {
        Result result = new Result();
        result.setSuccess(false);
        result.setMsg(msg);
        result.setCode(ResultCode.ERROR.code());
        result.setData(null);
        return result;
    }


    public static Result failure(ResultCode resultCode, Object data) {
        Result result = new Result();
        result.setSuccess(false);
        result.setResultCode(resultCode);
        result.setData(data);
        return result;
    }

    public void setResultCode(ResultCode code) {
        this.code = code.code();
        this.msg = code.message();
    }

}
